package cn.com.mdx.sort.heapsort.practice.taxicabnumbers;

import java.util.HashMap;
import java.util.Map;

/**
 * <p> 的士数
 *
 * @author ：huangy
 * @date ：Created in 2020/10/22 9:24 上午
 */
public class TaxicabNumbers {

    /**
     * @param N N以内的数
     * @return void
     * @Author huangy
     * @description //TODO 打印的士数 - 快速实现
     * @date 10:10 上午 2020/11/2
     **/
    public static void getTaxicabNumbers(long N) {
        if (N < 1729) System.out.println("1729以内不存在的士数");
        else {
            //用来存放立方和->数量
            Map<Integer, Integer> cubeSumToNum = new HashMap<>();
            int scopeNum = (int) Math.pow(N, (double) 1 / 3) + 1;
            for (int i = 1; i < scopeNum; i++) {
                for (int j = i + 1; j < scopeNum; j++) {
                    int cubeSum = (int) (Math.pow(i, 3) + Math.pow(j, 3));
                    Integer cubeSumNum = cubeSumToNum.get(cubeSum);
                    if (cubeSumNum == null) cubeSumToNum.put(cubeSum, 1);
                    else System.out.println("的士数：" + cubeSum);
                }
            }
        }
    }

    private static void getTaxicabNumbersForce(long N) {
        if (N < 1729) System.out.println("1729以内不存在的士数");
        else {
            Map<Integer, Integer> numToFlag = new HashMap<>();
            int scopeNum = (int) Math.pow(N, (double) 1 / 3) + 1;
            for (int i = 0; i < scopeNum; i++) {
                for (int j = 0; j < scopeNum; j++) {
                    if (i == j) continue;
                    int iAndJ3 = (int) (Math.pow(i, 3) + Math.pow(j, 3));
                    if (iAndJ3 > N) break;
                    for (int k = 0; k < scopeNum; k++) {
                        if (i == k || j == k) continue;
                        for (int l = 0; l < scopeNum; l++) {
                            if (i == l || j == l || k == l) continue;
                            int kAndL3 = (int) (Math.pow(k, 3) + Math.pow(l, 3));
                            if (iAndJ3 == kAndL3) {
                                if (numToFlag.get(iAndJ3) == null) {
                                    System.out.println("的士数：" + iAndJ3 + " = "
                                            + i + "^3 + " + j + "^3 = " + k + "^3 + " + l + "^3");
                                    numToFlag.put(iAndJ3, 1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int N = 50000;
        long time1 = System.currentTimeMillis();
        getTaxicabNumbersForce(N);
        long time2 = System.currentTimeMillis();
        getTaxicabNumbers(N);
        long time3 = System.currentTimeMillis();
        System.out.println("执行时间1：" + (time2 - time1));
        System.out.println("执行时间2：" + (time3 - time2));
    }

}
